﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="RadioButton.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.GUI.
//
//   It is released under the Microsoft Public License (Ms-PL).
//
//   You should have received a copy of the Microsoft Public License
//   along with starLiGHT.GUI. If not, see http://sle.codeplex.com/license.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.GUI are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 9631                  $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-05-03 11:00:42 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.GUI.Widgets
{
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

    public class RadioButton : ButtonBase, ICheckable
    {
        #region Private Members
        private bool checkStatus;
        #endregion

        #region Constructors
        public RadioButton(GuiManager manager, string name)
            : base(manager, name)
        {
        }

        public RadioButton(GuiManager manager, string name, string layout)
            : base(manager, name, layout)
        {
        }
        #endregion

        #region Events
        public event EventHandler Click;

        public event EventHandler CheckChanged;

        #endregion

        public bool Checked
        {
            get
            {
                return this.checkStatus;
            }

            set
            {
                if (value != this.checkStatus)
                {
                    this.checkStatus = value;
                    if (this.checkStatus)
                    {
                        SetState("Checked");
                    }
                    else
                    {
                        SetState("Normal");
                    }
                }

                this.OnCheckChanged(this, EventArgs.Empty);
            }
        }

        public override bool CanFocus
        {
            get
            {
                return true;
            }
        }

        public override void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (!e.WidgetUnderMouse)
            {
                return;
            }

            base.OnMouseUp(sender, e);

            OnClick(this, e);

            if (!this.Checked)
            {
                SetState("Checked");
                this.Checked = true;

                if (Parent != null && Parent is ContainerWidget)
                {
                    foreach (Widget w in ((ContainerWidget)Parent).Widgets)
                    {
                        if (w is RadioButton && w != this)
                        {
                            ((RadioButton)w).Checked = false;
                        }
                    }
                }
            }

            this.OnCheckChanged(this, e);
        }

        public void OnCheckChanged(object sender, EventArgs e)
        {
            if (this.CheckChanged != null)
            {
                this.CheckChanged(sender, e);
            }
        }
    }
}
